package com.elink.enginerring.dict;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.elink.enginerring.common.Constant;
import com.elink.enginerring.common.ErrorType;
import com.elink.enginerring.datasource.DataSourceContextHolder;
import com.elink.enginerring.datasource.DataSourceType;
import com.elink.enginerring.dict.co.DictCo;
import com.elink.enginerring.dict.entity.Dict;
import com.elink.enginerring.dict.service.IDictService;
import com.elink.enginerring.user.entity.User;
import com.elink.enginerring.util.IDGenerator;
import com.elink.enginerring.util.JSONUtil;

@Controller
@RequestMapping("view/dict")
@Api(value="词典接口")
@ResponseBody
public class DictApiController {

	@Autowired
	private IDictService dictService;
	
	/**
	 * 获取词典列表
	 * @param euserPhone
	 * @return
	 */
	@RequestMapping(value = "/getList", method = RequestMethod.GET)
	@ApiOperation(value="获取词典列表",httpMethod="GET")
	public Map<String, Object> getList(@ModelAttribute @Valid DictCo co,HttpSession   session) {
		DataSourceContextHolder.setDbType(DataSourceType.DBDICT);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		User user = 	(User) session.getAttribute(Constant.su_in_session);
		co.setUserId(user.getUserId());
		System.out.println("字段的值"+co.getByName()+"：长度"+co.getByName().length());
		return dictService.getListOfPage(co);
	}
	
	/**
	 * 获取词条详情
	 * @param dictId
	 * @return
	 */
	@RequestMapping(value = "/getInfo", method = RequestMethod.GET)
	@ApiOperation(value="获取词典详情",httpMethod="GET")
	public Map<String, Object> getInfo(int dictId) {
		DataSourceContextHolder.setDbType(DataSourceType.DBDICT);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return dictService.getInfo(dictId);
	}
	
	/**
	 *  添加词条
	 * @param dict
	 * @param merge 0 --合并词条 1--替换
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/addDict",method = RequestMethod.POST)
	public Map<String, Object> addDict(@ModelAttribute("Dict") @Valid Dict dict,@RequestParam(value="merge" ,required=false,defaultValue="")String merge,HttpSession   session) {
		DataSourceContextHolder.setDbType(DataSourceType.DBDICT);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		User user = 	(User) session.getAttribute(Constant.su_in_session);
		dict.setUserId(user.getUserId());
		return dictService.addDict(dict,merge);
	}
	
	/**
	 * 判断词条是否重复
	 * @param byName
	 * @return
	 */
	@RequestMapping(value="/getDistinct",method = RequestMethod.GET)
	public Map<String, Object>getDistinct(@RequestParam(value="byName")String byName){
		DataSourceContextHolder.setDbType(DataSourceType.DBDICT);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		return dictService.getDistinct(byName);
	}
	
	/**
	 * 修改词条
	 * @param dict
	 * @return
	 */
	@RequestMapping("/editDict")
	public Map<String, Object> editDict(@ModelAttribute("Dict") @Valid Dict dict) {
		DataSourceContextHolder.setDbType(DataSourceType.DBDICT);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		dictService.editDict(dict);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	/**
	 * 批量删除词条
	 * @param dictIds
	 * @return
	 */
	@RequestMapping("/delDict")
	public Map<String, Object> delDict(String dictIds) {
		DataSourceContextHolder.setDbType(DataSourceType.DBDICT);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		dictService.delDict(dictIds);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
}
